package com.tianqu.tms.www.member.controller;


import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.IntegralRegister;
import com.tianqu.tms.entity.IntegralcardUpdateHistory;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.member.viewhelper.MemberIntegralViewHelper;

/**
 * @author zhangxueliang
 * 积分信息查看 controller
 */
public class MemberIntegralDetailController extends TmsController{
	private Logger logger = Logger.getLogger(MemberIntegralDetailController.class);
	
	private static final String MEMBERID = "memId";// 会员ID
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			MemberIntegralViewHelper viewhelper = new MemberIntegralViewHelper();
			String memberId = (String) uc.getAttribute(MEMBERID);
			IntegralRegister integralRegister = IntegralRegister.getInstance();
			Member mem = Member.getInstance();
			integralRegister = integralRegister.findIntegralByMemId(memberId);
			if(integralRegister != null){
				viewhelper.setPriceAll(integralRegister.getPriceAll());
				viewhelper.setTansferPriceAll(integralRegister.getTransferPriceAll());
				viewhelper.setTransferAll(integralRegister.getTransferTime());
			}		
			mem = mem.findMemberInfoByMemId(memberId);
			if(mem != null){
				viewhelper.setAucMargin(mem.getAucMargin());
				viewhelper.setFragileStickerFreePort(mem.getFragileStickerFreePort());
				viewhelper.setMargin(mem.getMargin());
				viewhelper.setVirtualMargin(mem.getVirtualMargin());
				viewhelper.setIntegralRatio(mem.getIntegralRatio() * 100 +"");
				viewhelper.setAccessCardId(mem.getAccessCardId());
				viewhelper.setAccessCardNo(mem.getAccessCardNo());
				//累计积分(分)
				//viewhelper.setCardScore(mem.getCardScore());
				viewhelper.setCardScore(mem.getCardScore()+integralRegister.getUsedIntegral());
				viewhelper.setMemberId(memberId);
				//已用积分总数
				viewhelper.setUsedIntegralAll(integralRegister.getUsedIntegral());
				//剩余积分总数
				//viewhelper.setSurplusIntegralAll(mem.getCardScore() - integralRegister.getUsedIntegral());
				viewhelper.setSurplusIntegralAll(mem.getCardScore());
			}
			IntegralcardUpdateHistory history = IntegralcardUpdateHistory.getInstance();
			List historyList = history.findHistoryByMemId(memberId);
			viewhelper.setIntegralcardHistoryList(historyList);
			
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		
	}

}
